package com.community.contents.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.common.core.exception.CommunityException;
import com.community.common.core.utils.Result;
import com.community.common.security.dto.UserLoginDto;
import com.community.contents.dto.TopicDTO;
import com.community.contents.entity.PostEntity;
import com.community.contents.entity.TopicEntity;
import com.community.contents.entity.TopicUserEntity;
import com.community.contents.service.ITopicService;
import com.community.contents.service.ITopicUserService;
import com.community.contents.vo.TopicVo;
import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 圈子
 *
 * @author hjs hjs@qq.com
 * @since 1.0.0 2023-02-21
 */
@RestController
@RequestMapping("contents/topic")
@Api(tags="圈子")
public class TopicController {
    @Autowired
    private ITopicService topicService;
    @Autowired
    private ITopicUserService topicUserService;

    @PostMapping("page")
    public Result<Page<TopicDTO>> page(@RequestBody Page<TopicDTO> params){
        topicService.pageDto(params);
        return new Result<Page<TopicDTO>>().ok(params);
    }


    //添加圈子--view
    @PostMapping("save")
    @Transactional(rollbackFor = Exception.class,timeout = 6000)
    public Result save(@RequestBody TopicEntity dto){
        dto.setCreateTime(new Date());
        try {
            //添加圈子
            boolean save = topicService.save(dto);
            //添加用户关注的圈子
            TopicUserEntity topicUserEntity = new TopicUserEntity();
            topicUserEntity.setTopicId(dto.getId());
            topicUserEntity.setCreateTime(new Date());
            topicUserEntity.setUserId(Integer.parseInt(dto.getUserId()));
            topicUserService.save(topicUserEntity);
            return new Result<>().ViewOk(dto);
        }catch (Exception e){
           // throw new CommunityException("圈子名称已存在，请重新命名");
            e.printStackTrace();
            return new Result().error("圈子名称已存在，请重新命名");
        }
    }

    //圈子详情页跳转--view
    @GetMapping("detail")
    public Result detail(@RequestParam("id") Integer id){

        //QueryWrapper<TopicEntity> queryWrapper = new QueryWrapper<>();
        //queryWrapper.eq("id",id);
        TopicVo detail = topicService.detail(id);
        detail.setIsJoin(true);//创建圈子的默认加入改圈子
        return new Result<>().ViewOk(detail);
    }

    @GetMapping("postClassList")
    public void postClassList(@RequestParam Integer topicId){
        //List<PostEntity> list= topicService.postClassList(topicId);
    }

    //用户加入圈子--view
    @PostMapping("joinTopic")
    public  Result joinTopic(@RequestBody Map<String,Object> id, UserLoginDto userLoginDto){
        //QueryWrapper<TopicUserEntity> queryWrapper = new QueryWrapper();
        //queryWrapper.eq("topic_id",id);
        //queryWrapper.eq("user_id",userLoginDto.getUserId());
        //TopicUserEntity one = topicUserService.getOne(queryWrapper);
        //添加用户关注的圈子
        TopicUserEntity topicUserEntity = new TopicUserEntity();
        int id1 = Integer.parseInt((String) id.get("id"));
        topicUserEntity.setTopicId(id1);
        topicUserEntity.setCreateTime(new Date());
        topicUserEntity.setUserId(userLoginDto.getUserId());
        boolean save = topicUserService.save(topicUserEntity);
        return new Result().out(save);
    }

}